package com.cw.jeeyt.service.impl.api;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.service.intf.api.ApiFacadeDocService;
import com.cw.jeeyt.service.intf.api.ApiFacadeMethodService;
import com.cw.jeeyt.service.intf.api.ApiFacadeModuleInfoService;
import com.cw.jeeyt.service.intf.api.ApiFacadeProjectInfoService;
import com.cw.jeeyt.common.FileUploadUtil;
import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.common.constants.ApiConstants;
import com.cw.jeeyt.common.utils.ConvertSwf;
import com.cw.jeeyt.common.utils.FileUtils;
import com.cw.jeeyt.common.utils.PdfGenerateUtil;
import com.cw.jeeyt.common.utils.SystemPath;
import com.cw.jeeyt.service.bo.api.ApiFacadeDocBo;
import com.cw.jeeyt.service.bo.api.ApiFacadeDocDto;
import com.cw.jeeyt.service.bo.api.ApiFacadeDocExcel;
import com.cw.jeeyt.service.bo.api.ApiFacadeDocMethodExcel;
import com.cw.jeeyt.service.bo.api.ApiFacadeMethodBo;
import com.cw.jeeyt.service.bo.api.ApiFacadeModuleInfoBo;
import com.cw.jeeyt.service.bo.api.ApiFacadeProjectInfoBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemFileTemplateBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiFacadeDocDao;
import com.cw.jeeyt.dao.model.api.ApiFacadeDocDo;
import com.cw.jeeyt.service.converter.api.ApiFacadeDocConverter;
import com.cw.jeeyt.service.intf.system.SystemFileTemplateService;
import com.cw.lang.common.utils.PropertiesLoader;
import com.cw.lang.common.utils.SequenceTool;
import com.cw.lang.mybatis.dto.PageInfo;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;

/**
 * api接口文档业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiFacadeDocServiceImpl implements ApiFacadeDocService {

    private static final Logger logger = LoggerFactory.getLogger(ApiFacadeDocServiceImpl.class);
    @Autowired
    private ApiFacadeDocDao apiFacadeDocDao;
    @Autowired
    private ApiFacadeMethodService apiFacadeMethodService;
    @Autowired
    private SystemFileTemplateService systemFileTemplateService;
    @Autowired
    private ApiFacadeProjectInfoService apiFacadeProjectInfoService;
    @Autowired
    private ApiFacadeModuleInfoService apiFacadeModuleInfoService;

    /**
     * 查询所有api接口文档
     */
    @Override
    public List<ApiFacadeDocBo> queryApiFacadeDocList() throws Exception {
        return ApiFacadeDocConverter.doList2BoList(apiFacadeDocDao.queryApiFacadeDocList());
    }

    /**
     * 分页查询api接口文档
     *
     * @param apiFacadeDocBo
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiFacadeDocBo> queryByPage(ApiFacadeDocBo apiFacadeDocBo, Integer pageNo, Integer pageSize,
        Order order) throws Exception {

        if (apiFacadeDocBo == null) {
            logger.error("api接口文档为空");
            return null;
        }
        PageInfo<ApiFacadeDocDo> pageInfo = apiFacadeDocDao.queryByPage(ApiFacadeDocConverter.bo2Do(apiFacadeDocBo),
            pageNo, pageSize, order);
        return new PageInfo<>(pageInfo, ApiFacadeDocConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻api接口文档信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiFacadeDocBo getApiFacadeDocById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        ApiFacadeDocBo apiFacadeDocBo = ApiFacadeDocConverter.do2Bo(apiFacadeDocDao.getById(id));
        apiFacadeDocBo.setMethodList(apiFacadeMethodService.queryListByDocId(id));
        return apiFacadeDocBo;
    }

    /**
     * 新增api接口文档
     *
     * @param apiFacadeDocBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ApiFacadeDocBo apiFacadeDocBo) throws Exception {

        if (apiFacadeDocBo == null) {
            logger.error("api接口文档为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeDocDo apiFacadeDoc = ApiFacadeDocConverter.bo2Do(apiFacadeDocBo);
        apiFacadeDoc.setCreateBy(userVo.getId());
        apiFacadeDoc.setCreateDate(new Date());
        apiFacadeDoc.setUpdateBy(userVo.getId());
        apiFacadeDoc.setUpdateDate(new Date());
        Integer result = apiFacadeDocDao.insert(apiFacadeDoc);
        return result;
    }

    /**
     * 新增api接口文档不为空的字段
     *
     * @param apiFacadeDocBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiFacadeDocBo apiFacadeDocBo) throws Exception {

        if (apiFacadeDocBo == null) {
            logger.error("api接口文档为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeDocDo apiFacadeDoc = ApiFacadeDocConverter.bo2Do(apiFacadeDocBo);
        apiFacadeDoc.setCreateBy(userVo.getId());
        apiFacadeDoc.setCreateDate(new Date());
        apiFacadeDoc.setUpdateBy(userVo.getId());
        apiFacadeDoc.setUpdateDate(new Date());
        Integer result = apiFacadeDocDao.insertSelective(apiFacadeDoc);
        return result;
    }

    /**
     * 更新api接口文档
     *
     * @param apiFacadeDocBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiFacadeDocBo apiFacadeDocBo) throws Exception {

        if (apiFacadeDocBo == null) {
            logger.error("api接口文档为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeDocDo apiFacadeDoc = ApiFacadeDocConverter.bo2Do(apiFacadeDocBo);
        apiFacadeDoc.setUpdateBy(userVo.getId());
        apiFacadeDoc.setUpdateDate(new Date());

        apiFacadeDocDao.update(apiFacadeDoc);
    }

    /**
     * 更新api接口文档不为空的字段
     *
     * @param apiFacadeDocBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiFacadeDocBo apiFacadeDocBo) throws Exception {

        if (apiFacadeDocBo == null) {
            logger.error("api接口文档为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeDocDo apiFacadeDoc = ApiFacadeDocConverter.bo2Do(apiFacadeDocBo);
        apiFacadeDoc.setUpdateBy(userVo.getId());
        apiFacadeDoc.setUpdateDate(new Date());

        apiFacadeDocDao.updateSelective(apiFacadeDoc);
    }

    /**
     * 删除api接口文档
     *
     * @param apiFacadeDocBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiFacadeDocBo apiFacadeDocBo) throws Exception {

        if (apiFacadeDocBo == null) {
            logger.error("api接口文档为空");
            return;
        }
        apiFacadeDocDao.delete(ApiFacadeDocConverter.bo2Do(apiFacadeDocBo));
    }

    /**
     * 查询api接口文档
     *
     * @param requestData
     * @return
     * @throws Exception
     */
    @Override
    public List<ApiFacadeDocBo> searchResult(String requestData) throws Exception {
        if (StringUtils.isEmpty(requestData)) {
            logger.error("api接口文档查询参数为空");
            return null;
        }

        List<ApiFacadeDocBo> list_ApiFacadeDocBo = ApiFacadeDocConverter.doList2BoList(
            apiFacadeDocDao.searchResult(requestData));
        if (list_ApiFacadeDocBo != null && !list_ApiFacadeDocBo.isEmpty()) {
            for (ApiFacadeDocBo new_apiFacadeDoc : list_ApiFacadeDocBo) {
                new_apiFacadeDoc.setMethodList(apiFacadeMethodService.queryListByDocId(new_apiFacadeDoc.getId()));
            }
        }
        return list_ApiFacadeDocBo;
    }

    /**
     * 生成预览的接口文档pdf及swf文件
     *
     * @param docId
     */
    @Override
    public Map<String, Object> generateReviewFile(Long docId) {
        Map<String, Object> map = new HashMap<>();
        try {
            ApiFacadeDocBo apiFacadeDoc = this.getApiFacadeDocById(docId);
            //封装参数
            Map<String, Object> variables = initData(apiFacadeDoc);
            //获取模板路径、文件输出路径
            String systemPath = SystemPath.getSysPath();
            String pdfFileName = SequenceTool.nextId() + "_" + apiFacadeDoc.getId();
            SystemFileTemplateBo systemFileTemplate = systemFileTemplateService.querySystemFileTemplateByCode(
                ApiConstants.TEMPLATE_API_FACADE_DOC);
            String templateSavePath = systemPath + systemFileTemplate.getTemplateSavePath();
            String fileOutputPath = systemFileTemplate.getFileOutputPath();
            String pdfOutputPath = "";
            String swfOutputPath = "";
            pdfOutputPath = systemPath + fileOutputPath + File.separator + "pdf";
            swfOutputPath = systemPath + fileOutputPath + File.separator + "swf";
            PdfGenerateUtil.generatePDF(pdfFileName + ".pdf", ApiConstants.TEMPLATE_API_FACADE_DOC + ".ftl", variables,
                pdfOutputPath, templateSavePath);
            ConvertSwf.convertPDF(pdfFileName + ".pdf", pdfOutputPath, swfOutputPath);
            map.put("pdfFileName", pdfFileName);
            map.put("swfFileName", pdfFileName);
        } catch (Exception e) {
            logger.error("生成预览的接口文档PDF及swf文件出错");
            e.printStackTrace();
            return null;
        }
        return map;
    }

    /**
     * 封装生成PDF参数
     *
     * @param apiFacadeDoc
     * @return
     * @throws Exception
     */
    private Map<String, Object> initData(ApiFacadeDocBo apiFacadeDoc) throws Exception {
        Map<String, Object> variables = new HashMap<>();
        variables.put("apiFacadeDoc", apiFacadeDoc);//接口文档
        return variables;
    }

    /**
     * 下载文档
     */
    @Override
    public void downloadDoc(Long docId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String systemPath = SystemPath.getSysPath();
        Map<String, Object> pdfFileName = generateReviewFile(docId);//生成PDF及swf文件
        SystemFileTemplateBo systemFileTemplate = systemFileTemplateService.querySystemFileTemplateByCode(
            ApiConstants.TEMPLATE_API_FACADE_DOC);
        String fileOutputPath = systemFileTemplate.getFileOutputPath();
        String pdfOutputPath = systemPath + fileOutputPath + File.separator + "pdf";
        String pdfFile = pdfOutputPath + File.separator + pdfFileName.get("pdfFileName") + ".pdf";
        File file = new File(pdfFile);
        FileUtils.downFile(file, request, response);
    }

    /**
     * 导入接口文档
     */
    @Override
    public void importApiFacadeDoc(HttpServletRequest request) throws Exception {

        PropertiesLoader loader = new PropertiesLoader("classpath:application.properties");
        String uploadFilePath = loader.getProperty("uploadFile.path");
        List<String> filePathList = FileUploadUtil.uploadFile(request, uploadFilePath, uploadFilePath);
        if (filePathList != null && !filePathList.isEmpty()) {
            for (String filePath : filePathList) {
                //导入接口文档信息
                ImportParams params = new ImportParams();
                params.setTitleRows(1);
                params.setStartSheetIndex(0);
                List<ApiFacadeDocExcel> apiDocList = ExcelImportUtil.importExcel(new File(filePath),
                    ApiFacadeDocExcel.class, params);
                if (apiDocList != null && !apiDocList.isEmpty()) {
                    saveImportApiFacadeDoc(apiDocList);
                }
                //导入接口文档方法信息
                params = new ImportParams();
                params.setTitleRows(1);
                params.setStartSheetIndex(1);
                List<ApiFacadeDocMethodExcel> apiDocMethodList = ExcelImportUtil.importExcel(new File(filePath),
                    ApiFacadeDocMethodExcel.class, params);
                if (apiDocMethodList != null && !apiDocMethodList.isEmpty()) {
                    saveImportApiFacadeDocMethod(apiDocMethodList);
                }
            }
        }
    }

    /**
     * 保存导入的接口文档相关信息
     *
     * @param apiDocList
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveImportApiFacadeDoc(List<ApiFacadeDocExcel> apiDocList) throws Exception {

        //接口文档信息
        List<ApiFacadeDocBo> apiFacadeDocList = new ArrayList<ApiFacadeDocBo>();
        //接口项目信息
        List<ApiFacadeProjectInfoBo> apiFacadeProjectInfoList = new ArrayList<ApiFacadeProjectInfoBo>();
        //接口模块信息
        List<ApiFacadeModuleInfoBo> apiFacadeModuleInfoList = new ArrayList<ApiFacadeModuleInfoBo>();
        List<String> projectCodeList = new ArrayList<String>();
        List<String> moduleCodeList = new ArrayList<String>();
        if (apiDocList != null && !apiDocList.isEmpty()) {
            for (ApiFacadeDocExcel apiFacadeDocPojo : apiDocList) {

                ApiFacadeProjectInfoBo apiFacadeProject = new ApiFacadeProjectInfoBo();
                ApiFacadeModuleInfoBo apiFacadeModule = new ApiFacadeModuleInfoBo();
                ApiFacadeDocBo baseApiFacadeDoc = new ApiFacadeDocBo();
                BeanUtils.copyProperties(apiFacadeDocPojo, apiFacadeProject);
                apiFacadeProject.setProjectComment(apiFacadeDocPojo.getProjectName());
                BeanUtils.copyProperties(apiFacadeDocPojo, apiFacadeModule);
                apiFacadeModule.setModuleComment(apiFacadeDocPojo.getModuleName());
                BeanUtils.copyProperties(apiFacadeDocPojo, baseApiFacadeDoc);
                String apiFacadeCodes = apiFacadeDocPojo.getApiFacadeCodes();
                String apiFacadeNames = apiFacadeDocPojo.getApiFacadeNames();
                if (StringUtils.isEmpty(apiFacadeCodes) || StringUtils.isEmpty(apiFacadeNames)) {
                    logger.error("接口编码或接口名称不能为空");
                    throw new Exception("接口编码或接口名称不能为空，请确认导入的文档内容是否正确！");
                }
                String[] apiFacadeCodeList = apiFacadeCodes.split("\n");
                String[] apiFacadeNameList = apiFacadeNames.split("\n");
                //判断接口编码与接口名称长度是否一致，不一致则抛异常
                if (apiFacadeCodeList.length != apiFacadeNameList.length) {
                    throw new Exception("接口编码与接口名称数量不一致，请确认导入的文档内容是否正确！");
                }
                int i = 0;
                for (String apiFacadeCode : apiFacadeCodeList) {
                    ApiFacadeDocBo apiFacadeDoc = new ApiFacadeDocBo();
                    BeanUtils.copyProperties(baseApiFacadeDoc, apiFacadeDoc);
                    apiFacadeDoc.setApiFacadeCode(apiFacadeCode);
                    apiFacadeDoc.setApiFacadeName(apiFacadeNameList[i]);
                    //校验接口文档是否存在，不存在才新增
                    ApiFacadeDocBo checkDoc = this.getApiFacadeDocByDocCode(apiFacadeCode);
                    if (checkDoc == null || null == checkDoc.getId()) {
                        apiFacadeDocList.add(apiFacadeDoc);
                    }
                    i++;
                }
                //校验接口项目信息是否存在，不存在则新增
                List<String> projectCheckCodeList = new ArrayList<String>();
                projectCheckCodeList.add(baseApiFacadeDoc.getProjectCode());
                List<ApiFacadeProjectInfoBo> projectInfoList = apiFacadeProjectInfoService.queryListByProjectCodes(
                    projectCheckCodeList);
                if (projectInfoList == null || projectInfoList.isEmpty()) {
                    apiFacadeProjectInfoList.add(apiFacadeProject);
                }
                if (!projectCodeList.contains(apiFacadeDocPojo.getProjectCode())) {
                    projectCodeList.add(apiFacadeDocPojo.getProjectCode());
                }
                //校验接口模块信息是否存在，不存在则新增
                List<String> moduleCheckCodeList = new ArrayList<String>();
                moduleCheckCodeList.add(baseApiFacadeDoc.getModuleCode());
                List<ApiFacadeModuleInfoBo> moduleInfoList = apiFacadeModuleInfoService.queryListByModuleCodes(
                    moduleCheckCodeList);
                if (moduleInfoList == null || moduleInfoList.isEmpty()) {
                    apiFacadeModuleInfoList.add(apiFacadeModule);
                }
                if (!moduleCodeList.contains(apiFacadeDocPojo.getModuleCode())) {
                    moduleCodeList.add(apiFacadeDocPojo.getModuleCode());
                }
            }
        }
        //批量保存接口项目信息
        if (apiFacadeProjectInfoList != null && !apiFacadeProjectInfoList.isEmpty()) {
            apiFacadeProjectInfoService.saveApiProjectInfoBat(apiFacadeProjectInfoList);
        }
        List<ApiFacadeProjectInfoBo> projectInfoList = apiFacadeProjectInfoService.queryListByProjectCodes(
            projectCodeList);
        if (projectInfoList != null && !projectInfoList.isEmpty()) {
            for (ApiFacadeProjectInfoBo projectInfo : projectInfoList) {
                for (ApiFacadeModuleInfoBo moduleInfo : apiFacadeModuleInfoList) {
                    if (projectInfo.getProjectCode()
                        .equals(moduleInfo.getProjectCode())) {
                        moduleInfo.setProjectId(projectInfo.getId());
                        continue;
                    }
                }
            }
            //批量保存接口模块信息
            if (apiFacadeModuleInfoList != null && !apiFacadeModuleInfoList.isEmpty()) {
                apiFacadeModuleInfoService.saveApiModuleInfoBat(apiFacadeModuleInfoList);
            }
            List<ApiFacadeModuleInfoBo> moduleInfoList = apiFacadeModuleInfoService.queryListByModuleCodes(
                moduleCodeList);
            if (moduleInfoList != null && !moduleInfoList.isEmpty()) {
                for (ApiFacadeModuleInfoBo moduleInfo : moduleInfoList) {
                    for (ApiFacadeDocBo apiFacadeDoc : apiFacadeDocList) {
                        if (moduleInfo.getModuleCode()
                            .equals(apiFacadeDoc.getModuleCode())) {
                            apiFacadeDoc.setModuleId(moduleInfo.getId());
                            apiFacadeDoc.setProjectId(moduleInfo.getProjectId());
                            continue;
                        }
                    }
                }
                //批量保存接口文档信息
                if (apiFacadeDocList != null && !apiFacadeDocList.isEmpty()) {
                    this.saveApiFacadeDocBat(apiFacadeDocList);
                }
            }
        }
    }

    /**
     * 批量保存接口文档信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveApiFacadeDocBat(List<ApiFacadeDocBo> apiFacadeDocList) throws Exception {

        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        if (apiFacadeDocList != null && !apiFacadeDocList.isEmpty()) {
            List<ApiFacadeDocDo> apiDocList = new ArrayList<ApiFacadeDocDo>();
            for (ApiFacadeDocBo afd : apiFacadeDocList) {
                ApiFacadeDocDo apiFacade = ApiFacadeDocConverter.bo2Do(afd);
                apiFacade.setCreateBy(userVo.getId());
                apiFacade.setCreateDate(new Date());
                apiFacade.setUpdateBy(userVo.getId());
                apiFacade.setUpdateDate(new Date());
                apiDocList.add(apiFacade);
            }
            apiFacadeDocDao.insertBat(apiDocList);
        }
    }

    /**
     * 保存接口文档方法信息
     *
     * @param apiDocMethodList
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveImportApiFacadeDocMethod(List<ApiFacadeDocMethodExcel> apiDocMethodList) throws Exception {

        //保存接口文档方法信息
        List<ApiFacadeMethodBo> apiFacadeMethodList = new ArrayList<ApiFacadeMethodBo>();
        if (apiDocMethodList != null && !apiDocMethodList.isEmpty()) {
            for (ApiFacadeDocMethodExcel apiFacadeDocMethod : apiDocMethodList) {
                ApiFacadeMethodBo baseApiFacadeMethod = new ApiFacadeMethodBo();
                BeanUtils.copyProperties(apiFacadeDocMethod, baseApiFacadeMethod);
                //根据接口文档编码查询接口文档id
                ApiFacadeDocBo apiFacadeDoc = this.getApiFacadeDocByDocCode(apiFacadeDocMethod.getApiFacadeCode());
                if (apiFacadeDoc == null) {
                    throw new Exception(baseApiFacadeMethod.getApiFacadeCode() + "接口文档信息不存在，请确认导入文档的接口编码是否正确或已填写！");
                }
                baseApiFacadeMethod.setDocId(apiFacadeDoc.getId());
                String methodCodes = apiFacadeDocMethod.getMethodCodes();
                String methodNames = apiFacadeDocMethod.getMethodNames();
                String requests = apiFacadeDocMethod.getRequests();
                String responses = apiFacadeDocMethod.getResponses();
                if (StringUtils.isEmpty(methodCodes) || StringUtils.isEmpty(methodNames) || StringUtils.isEmpty(
                    requests) || StringUtils.isEmpty(responses)) {
                    throw new Exception(baseApiFacadeMethod.getApiFacadeCode() + "接口文档方法信息不完整，请确认文档内容是否录入完整！");
                }
                String[] methodCodeList = methodCodes.split("\n");
                String[] methodNameList = methodNames.split("\n");
                String[] requestList = requests.split("\n");
                String[] responseList = responses.split("\n");
                if (methodCodeList.length != methodNameList.length || methodCodeList.length != requestList.length
                    || methodCodeList.length != responseList.length) {
                    throw new Exception(baseApiFacadeMethod.getApiFacadeCode() + "接口文档方法相关数量不一致，请确认录入是否正确。");
                }
                for (int i = 0; i < methodCodeList.length; i++) {
                    ApiFacadeMethodBo apoFacadeMethod = new ApiFacadeMethodBo();
                    BeanUtils.copyProperties(baseApiFacadeMethod, apoFacadeMethod);
                    apoFacadeMethod.setMethodCode(methodCodeList[i]);
                    apoFacadeMethod.setMethodName(methodNameList[i]);
                    apoFacadeMethod.setRequest(requestList[i]);
                    apoFacadeMethod.setResponse(responseList[i]);
                    apiFacadeMethodList.add(apoFacadeMethod);
                }
            }
        }
        apiFacadeMethodService.saveApiFacadeMethodBat(apiFacadeMethodList);
    }

    /**
     * 根据接口编码查询接口文档信息
     *
     * @param apiDocCode
     * @return
     */
    @Override
    public ApiFacadeDocBo getApiFacadeDocByDocCode(String apiDocCode) throws Exception {
        if (StringUtils.isEmpty(apiDocCode)) {
            logger.error("--接口文档编码为空--");
            return null;
        }
        return ApiFacadeDocConverter.do2Bo(apiFacadeDocDao.getApiFacadeDocByDocCode(apiDocCode));
    }

    /**
     * 初始化搜索界面数据
     */
    @Override
    public JsonResultDto initData() throws Exception {
        Map<String, Object> params = new HashMap<>();
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        //查询所有接口项目信息
        List<ApiFacadeProjectInfoBo> projectInfoList = apiFacadeProjectInfoService.queryApiFacadeProjectInfoList();
        List<Map<String, Object>> projectList = new ArrayList<Map<String, Object>>();
        if (projectInfoList != null && !projectInfoList.isEmpty()) {
            int i = 0;
            for (ApiFacadeProjectInfoBo project : projectInfoList) {
                Map<String, Object> data = new HashMap<>();
                data.put("project_name", project.getProjectName());
                data.put("project_code", project.getProjectCode());
                projectList.add(data);
                if (i == 0) {
                    Map<String, Object> projectInfo = new HashMap<>();
                    projectInfo.put("project_name", project.getProjectName());
                    projectInfo.put("project_code", project.getProjectCode());
                    params.put("projectInfo", projectInfo);
                    List<ApiFacadeDocDto> apiFacadeDocList = this.queryListByProjectCode(project.getProjectCode());
                    params.put("apiDocList", apiFacadeDocList);
                }
                i++;
            }
        }
        params.put("projectList", projectList);
        jsonResultDto.setResultData(params);
        jsonResultDto.setSuccess(true);
        return jsonResultDto;
    }

    /**
     * 根据项目id查询项目下的接口信息
     *
     * @param projectCode
     * @return
     */
    @Override
    public List<ApiFacadeDocDto> queryListByProjectCode(String projectCode) {

        List<ApiFacadeDocDo> apiList = apiFacadeDocDao.queryListByProjectCode(projectCode);
        List<ApiFacadeDocDto> apiPojoList = new ArrayList<ApiFacadeDocDto>();
        if (apiList != null && !apiList.isEmpty()) {
            for (ApiFacadeDocDo apiFacadeDoc : apiList) {
                ApiFacadeDocDto doc = new ApiFacadeDocDto();
                doc.setDocId(apiFacadeDoc.getId());
                doc.setDocCode(apiFacadeDoc.getApiFacadeCode());
                doc.setDocName(apiFacadeDoc.getApiFacadeName());
                doc.setModuleCode(apiFacadeDoc.getModuleCode());
                doc.setModuleName(apiFacadeDoc.getModuleName());
                apiPojoList.add(doc);
            }
        }
        return apiPojoList;
    }
}
